package com.example.fragement;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import org.greenrobot.eventbus.EventBus;

public class Diy_Fragement_2 extends Fragment {
    public static final String BUNDLE_TITLE = "bundle_title";
    private String title;
    private int count;

    public static Diy_Fragement_2 newinstance(String title) {
        Diy_Fragement_2 fragement_2 = new Diy_Fragement_2();
        Bundle bundle = new Bundle();
        bundle.putString(BUNDLE_TITLE, title);
        fragement_2.setArguments(bundle);
        return fragement_2;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //在創建Fragment的時候就拿到title，通过全局变量在oncreateview的时候给textview使用
        if (getArguments() != null) {
            title = getArguments().getString(BUNDLE_TITLE);
        }

    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.diy_fragement_2, container, false);
        TextView textView = view.findViewById(R.id.fragement2);
        textView.setText(title);
        count=4;
        EventBus.getDefault().post(count);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (OntitleClickListener != null) {
                    OntitleClickListener.onFragment2Click(title);
                }
            }
        });
        return view;
    }


    public void setOntitleClickListener(Diy_Fragement_2.OntitleClickListener ontitleClickListener) {
        OntitleClickListener = ontitleClickListener;
    }

    OntitleClickListener OntitleClickListener;

    public interface OntitleClickListener {
        void onFragment2Click(String title);
    }
}
